from typing import Type, List

import peewee


class BaseDBModel(peewee.Model):
    """
    数据库基本Model
    """

    def to_dict(self):
        return self.__data__

    @classmethod
    def save_all(cls,objs: List['BaseDBModel']):
        dicts = [i.to_dict() for i in objs]
        self_model = cls
        db = self_model._meta.database
        with db.atomic():
            for c in peewee.chunked(dicts, 50):
                self_model.insert_many(
                    c).on_conflict_replace().execute()
